import box/{condit,logger}

config=$TTB_CONFIG/yaml/termux_property.yaml
tmp=$config.tmp
properties=$config.properties
keyboard=$TTB_CONFIG/other/keyboard.properties

# YAML
    # 校验非法键
    Has-File $tmp || cp $config $tmp
    Has-File $keyboard || touch $keyboard
    keys=($(yq '.property' $config -Pop | grep -vE '^# .+$' | awk '{print $1}'))

    if [ -z $2 ];then
        less ~/.termux/termux.properties
        exit
    fi
    OPTS=$(getopt --long env:,help -- "$@" 2>/dev/null)
    return=$?
    if ! ((return));then
        eval set -- "$OPTS"
        while true;do
            case "$1" in
                --env)
                    if [ -z $2 ];then
                        Err "未指定键值对"
                        exit
                    fi
                    num=0
                    IFS== read -r arg1 arg2 <<<"$2"
                    if [ -z $arg2 ];then
                        Err "未指定值"
                        exit
                    fi
                    for i in ${keys[@]};do
                        if In-Value i == arg1;then
                            yq -i ".property.[\"$arg1\"] = \"${arg2//\\s/ }\"" $tmp
                            let num++
                            break
                        fi
                    done
                    if ! ((num));then
                        Err "非法的键 $arg1"
                        exit
                    fi
                    shift 2
                ;;
                --help)
                    echo "用法: setting --env key1=value1 --env key2=value2..."
                    echo "设置 \$HOME/.termux/termux.properties 中的的项"
                    echo "无参数时将预览现有配置"
                    echo "值中有空格需使用\s代替"
                    shift
                    exit
                ;;
                --)
                    shift
                    break
            esac
        done
        yq '.property' $tmp -Pop > $properties
        cat $keyboard $properties > ~/.termux/termux.properties
        termux-reload-settings
    else
        Err "参数处理失败..."
        exit
    fi
    rm $properties